package com.learning.imoney;

import static android.app.PendingIntent.getActivity;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.learning.imoney.db.DataBaseBill;

import java.util.Calendar;

public class BillEdit extends AppCompatActivity implements View.OnClickListener {
    private EditText edMoney, edNote;
    private TextView edDate, dateText;
    private RadioGroup radioGroup;
    private Button btnSave, btnDelete, btnBack, dateBtn;
    private DataBaseBill mhelper;
    private SQLiteDatabase db;
    private int id;
    private Calendar calendar = Calendar.getInstance();


    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_billl_edit);
        //创建或获取数据库
        mhelper = new DataBaseBill(BillEdit.this);//实例化数据库帮助类
        db = mhelper.getWritableDatabase();//创建数据库，获取数据库的读写权限

        edMoney = findViewById(R.id.edit_money_text);
        edDate = findViewById(R.id.date_text);
        edNote = findViewById(R.id.ed_note_text);
        radioGroup = findViewById(R.id.rg2);
        id = getIntent().getIntExtra("id", 0);

        edMoney.setText(String.valueOf(getIntent().getDoubleExtra("money", 0)));
        edDate.setText(getIntent().getStringExtra("date"));
        edNote.setText(getIntent().getStringExtra("note"));
        if (getIntent().getIntExtra("type", 0) == 1) {
            radioGroup.check(R.id.ed_income);
        } else if (getIntent().getIntExtra("type", 0) == 0) {
            radioGroup.check(R.id.ed_expenditures);
        }


        btnBack = findViewById(R.id.back_button);
        btnDelete = findViewById(R.id.btn_delete);
        btnSave = findViewById(R.id.ed_btn_save);
        dateBtn = findViewById(R.id.pick_button);
        dateText = findViewById(R.id.date_text);

        btnBack.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        btnSave.setOnClickListener(this);
        dateText.setOnClickListener(this);
        dateBtn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.back_button) {
            finish();
        } else if (v.getId() == R.id.btn_delete) {
            db.delete("bill_info", "_id=?", new String[]{String.valueOf(id)});
            finish();
        } else if (v.getId() == R.id.ed_btn_save) {
            updateBill();
        } else if (v.getId() == R.id.pick_button || v.getId() == R.id.date_text) {
            datePick(v);
        }

    }

    private void updateBill() {
        int repeatOptionId = radioGroup.getCheckedRadioButtonId();
        ContentValues values = new ContentValues();
        values.put("money", edMoney.getText().toString());//
        values.put("time", edDate.getText().toString());//
        values.put("note", edNote.getText().toString());//
        if (repeatOptionId == R.id.ed_income) {
            values.put("type", 1);//
        } else if (repeatOptionId == R.id.ed_expenditures) {
            values.put("type", 0);//
        }
        db.update("bill_info", values, "_id=?", new String[]{String.valueOf(id)});
        finish();
        Toast.makeText(this,"账单已更新",Toast.LENGTH_SHORT).show();
    }

    private void datePick(View v) {
        Calendar now = Calendar.getInstance();
        int year = now.get(Calendar.YEAR);
        int month = now.get(Calendar.MONTH);
        int day = now.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog datePickerDialog = new DatePickerDialog(BillEdit.this,
                (view, year1, monthOfYear, dayOfMonth) -> {
                    calendar.set(Calendar.YEAR, year1);
                    calendar.set(Calendar.MONTH, monthOfYear);
                    calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                    dateText.setText(DateFormat.format("yyyy-MM-dd", calendar.getTime()));
                }, year, month, day);
        datePickerDialog.show();
    }
}